package com.sm.controller.minio;

import com.sm.minio.Minio;
import com.sm.util.Msg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author wangpengliang
 * Date 2020/5/20 4:40 下午
 * Copyright (C) www.lovesfang.com
 */
@Slf4j
@RestController
@RequestMapping("/file")
public class MinioController {


    @PostMapping(value = "/upload")
    public Msg upload(HttpServletRequest request) {
        log.info("进入upload");
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        if (fileMap.size() == 0) {
            return Msg.fail().add("msg", "请选择文件");
        }
        Collection<MultipartFile> files = fileMap.values();
        List<String> fileList = Minio.minioUpload(files.toArray(new MultipartFile[0]), Minio.BUCKET_SHARE_VIDEO);
        return Msg.success(fileList);
    }


    @PostMapping(value = "/uploadImage")
    public Msg uploadImage(HttpServletRequest request) {
        log.info("进入uploadImage");
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        if (fileMap.size() == 0) {
            return Msg.fail().add("msg","请选择文件");
        }
        Collection<MultipartFile> files = fileMap.values();
        List<String> fileList = Minio.minioUpload(files.toArray(new MultipartFile[0]),Minio.BUCKET_SHARE_IMAGE);
        return Msg.success(fileList);
    }

}

